package com.treetao.union.web.controller.mall;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.treetao.union.db.Const;
import com.treetao.union.db.Const.OrderStatus;
import com.treetao.union.db.model.MallOrder;
import com.treetao.union.db.model.MallOrderDetail;
import com.treetao.union.db.model.MallOrderExpress;
import com.treetao.union.db.model.MallOrderRecord;
import com.treetao.union.db.model.vo.LayerData;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.db.service.MallOrderDetailService;
import com.treetao.union.db.service.MallOrderExpressService;
import com.treetao.union.db.service.MallOrderRecordService;
import com.treetao.union.db.service.MallOrderService;
import com.treetao.union.web.constant.Constants;

/**
 * <p>
 * 订单主表 前端控制器
 * </p>
 *
 * @author Dev.M
 * @since 2020-04-11
 */
@Controller
@RequestMapping(Constants.AMIND_PREFIX + "/mall-order")
public class MallOrderController {

	@Autowired
	private MallOrderService mallOrderService;

	@Autowired
	private MallOrderDetailService mallOrderDetailService;
	@Autowired
	private MallOrderExpressService mallOrderExpressService;
	@Autowired
	private MallOrderRecordService mallOrderRecordService;

	@GetMapping("list")
	public String list(Model model) {
		model.addAttribute("statusMap", Const.status);
		return "/admin/mall-order/list";
	}

	@PostMapping("list")
	@ResponseBody
	public LayerData<MallOrder> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, ServletRequest request) {
		Map map = WebUtils.getParametersStartingWith(request, "s_");
		LayerData<MallOrder> layerData = new LayerData<>();
		QueryWrapper<MallOrder> wrapper = new QueryWrapper<MallOrder>();
		if (!map.isEmpty()) {
			String status = (String) map.get("status");
			if (StringUtils.isNotEmpty(status)) {
				wrapper.eq("status", Integer.valueOf(status));
			} else {
				map.remove("status");
			}
			String orderno = (String) map.get("orderno");
			if (StringUtils.isNotEmpty(orderno)) {
				wrapper.eq("orderno", Long.valueOf(orderno));
			} else {
				map.remove("orderno");
			}
		}
		wrapper.orderByDesc("create_time");
		IPage<MallOrder> pageData = mallOrderService.page(new Page<>(page, limit), wrapper);
		if (pageData.getRecords() != null) {
			for (MallOrder order : pageData.getRecords()) {
				Map<String, Object> ext = Maps.newHashMap();
				ext.put("orderStatusStr", OrderStatus.getStatusStr(order.getStatus()));
				order.setExts(ext);
			}
		}
		layerData.setData(pageData.getRecords());
		layerData.setCount(pageData.getTotal());
		return layerData;
	}

	@GetMapping("detail")
	public String detail(Long id, Model model) {
		MallOrder order = mallOrderService.getById(id);
		model.addAttribute("order", order);

		if (order != null) {
			Map<String, Object> exts = order.getExts();
			exts.put("orderStatusStr", OrderStatus.getStatusStr(order.getStatus()));
			order.setExts(exts);
			// details
			List<MallOrderDetail> details = mallOrderDetailService.getDetails(order.getOrderno());
			order.setDetails(details);

			MallOrderExpress express = mallOrderExpressService.getOrderExpress(order.getOrderno());
			order.setExpress(express);

			List<MallOrderRecord> records = mallOrderRecordService.getOrderRecord(order.getOrderno());
			order.setRecords(records);
		}

		return "/admin/mall-order/detail";
	}

	@PostMapping("send")
	public @ResponseBody RestResponse send(MallOrderExpress express) {
		if (express.getOrderno() == null) {
			return RestResponse.failure("订单号不能为空");
		}
		if (StringUtils.isEmpty(express.getCompanyNo())) {
			return RestResponse.failure("物流公司名称不能为空");
		}
		if (StringUtils.isEmpty(express.getExpressNo())) {
			return RestResponse.failure("物流单号不能为空");
		}
		MallOrder order = mallOrderService.getMallOrder(express.getOrderno());
		if (order == null) {
			return RestResponse.failure("订单号不存在");
		}
		if (order.getStatus() != OrderStatus.PREPAYED.getValue() && order.getStatus() != OrderStatus.PAYED.getValue()) {
			return RestResponse.failure("当前订单号不能发货");
		}
		boolean flag = mallOrderService.send(order.getOrderno(), express.getCompanyNo(), express.getExpressNo());
		return RestResponse.success(flag);
	}
}